Skip to content

fix: do not crash when a persisted VPN protocol is unavailable - #43

Open
Miixms99 wants to merge 1 commit into
ProtonVPN:stablefrom
Miixms99:cursor/fix-persisted-protocol-startup-3ef3
Open

Miixms99 wants to merge 1 commit into
ProtonVPN:stablefrom
Miixms99:cursor/fix-persisted-protocol-startup-3ef3

Conversation

@Miixms99

Copy link
Copy Markdown

Fixes #33.

Problem

VPNConnector._get_current_connection() uses if protocol := self._registry.get(...), expecting None when the persisted protocol has no valid implementation. Registry.get() raises RuntimeError instead (proton/vpn/core/registry.py). initialize_state() then crashes when connection_persistence.json names a protocol that is not registered or whose validate() returns false, such as a leftover protun-smart connection in snap/flatpak or on older NetworkManager.

Reproduce

  1. Leave connection_persistence.json pointing at protun-smart.
  2. Start where that protocol is missing or validate() is false.

Result: RuntimeError: No valid implementation found for protun-smart. Expected initial state: Disconnected.

Change

Catch that RuntimeError in _get_current_connection(), log a warning, and return None. Startup stays Disconnected, as for a missing or corrupt persistence file. The file is left on disk, matching the corrupt-file path. Registry.get() is unchanged: connect() and protun default selection depend on the exception.

Testing

Environment: Linux, Python 3.12.3, pytest 9.2.0, pytest-asyncio 1.5.0. Protocols are stub Registry classes; no VPN is opened.

Before the fix:

python3 -m pytest tests/python/core/test_vpnconnector_persistence.py --no-cov -q --tb=line

FF...
FAILED ...cannot_be_resolved[False]
RuntimeError: No valid implementation found for protun-smart
FAILED ...cannot_be_resolved[True]
RuntimeError: No valid implementation found for protun-smart
2 failed, 3 passed

[False] is not registered. [True] is registered with validate() false. Missing file, corrupt file, and a valid wireguard persistence passed.

After the fix, the same command: 5 passed in 0.10s.

Full suite: python3 -m pytest --tb=line -q -> 637 passed, 1 failed. The failure is tests/python/test_modules_exist.py::test_modules_exist (No module named proton.vpn.platform.protun). The public protun crate lacks this repo's pinned commit and does not compile here, so that module was not built. It is unrelated to this change.

flake8 on the two changed files exited 0. pylint has no project config; vpnconnector.py still has only the too-many-positional-arguments messages already present on stable.

AI assistance was used to write the change and run the commands above in the environment described; the outputs shown are the actual results.

Registry.get() raises RuntimeError when connection_persistence.json names
a protocol that is missing or no longer valid. Catch that during connector
startup, log a warning, and continue as if nothing was persisted.

Co-authored-by: Miixms <Miixms99@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

VPNConnector fails to initialize when the persisted connection uses a protocol that is no longer valid

2 participants